package com.soft.sitegenerator.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.soft.sitegenerator.Component;
import com.soft.sitegenerator.DataBaseComponent;
import com.soft.sitegenerator.business.hibernate.WebSite;
import com.soft.sitegenerator.exceptions.ObjectNotFoundException;
import com.soft.sitegenerator.form.PublishSiteForm;
import com.soft.sitegenerator.ftp.FtpDroplet;

public class PublishSiteAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		String publicar = request.getParameter("hacer");	
		Component component = new DataBaseComponent();		
		if (publicar != null){
			
			PublishSiteForm psForm = (PublishSiteForm) form;
			
			WebSite webSite = (WebSite) component.getBean("webSitesHib");
			webSite.setId(psForm.getIdWebSite());
			
			try {
				webSite.load();
			} catch (ObjectNotFoundException e) {
				e.printStackTrace();
			}
			
			FtpDroplet.ftp(psForm.getHostName(), psForm.getUserName(), psForm.getUserPass(), psForm.getDestFolder(), request.getContextPath()+"/resources/webapp/"+webSite.getName());		
			
			return mapping.findForward("fin");
		}
		else{
			
			WebSite webSite = (WebSite) component.getBean("webSitesHib");
			
			PublishSiteForm psForm = (PublishSiteForm) form;
			
			psForm.setSites(webSite.findAllByUser((Long) request.getSession().getAttribute("USER_LOGIN")));
			
			return mapping.findForward("publicar");
		}
		
	}
}